//定义周，根据获取日期转为周
var today = new Array('周日', '周一', '周二', '周三', '周四', '周五', '周六');
//定义swiper
var swiper;
//new日期
var myCalendar = new SimpleCalendar('#calendar');
$(function () {
    //获取月
    var monthCH = $('.sc-select-month').text();
    $(".month_span").text(monthCH)
    // console.log(monthCH)
    $(".sc-mleft").click(function () {
        myCalendar.subMonth();
        //获取年
        var year = $('.sc-select-year').text();
        console.log(year)
        var monthCH = $('.sc-select-month').text();
        $(".month_span").text(monthCH)
        var month = SimpleCalendar.prototype.languageData.months_CH.indexOf(monthCH) + 1;
    })
    var tyear = new Date().getFullYear();
    var tmonth = new Date().getMonth() + 1;
    var tdate = parseInt(tyear + '' + tmonth);
    console.log(tdate)
    $(".sc-mright").click(function () {
        var year_month = myCalendar.addMonthBeforehand();
        console.log(year_month)
        //var year = $('.sc-select-year').text();
        var monthCH = $('.sc-select-month').text();
        //var month = SimpleCalendar.prototype.languageData.months_CH.indexOf(monthCH) + 1;

        if (parseInt(year_month) > tdate) {
            return
        } else {
            myCalendar.addMonth();
            if (year_month.slice(4, 6).length == 1) {
                $(".month_span").text('0' + year_month.slice(4, 6))
            } else {
                $(".month_span").text(year_month.slice(4, 6))
            }

        }

    })

    //刷新新数据
    myCalendar._defaultOptions.mark = mark;
    myCalendar.update();
    //显示当天的活动在初始化mark之后
    //初始化今天的活动
    announceList($('.sc-today'));
    //有标记的日期点击事件
    $('#calendar').on("click", '.sc-selected', function () {
        announceList($(this));
    });
    //获取今天之后的日期，禁止选中
    var nextAll = $(".sc-today").nextAll();
    nextAll.on("click", function () {
        $(".sc-today").nextAll().removeClass('sc-selected')
        $(".sc-today").nextAll().removeClass('sc-active-day')
    });

    var day = new Date(tyear, tmonth - 1, 1);
    var week = day.getDay();
    $(".botarr").click(function () {
        $(".sc-body").removeClass("showmonth7")
        if (week == 0) week = 7;
        if ((week == 7 && (tmonth == 1 || tmonth == 3 || tmonth == 5 || tmonth == 7 || tmonth == 8 || tmonth == 10 || tmonth == 12))
            || (week == 6 && (tmonth == 1 || tmonth == 3 || tmonth == 5 || tmonth == 7 || tmonth == 8 || tmonth == 10 || tmonth == 12))
            || (week == 7 && (tmonth == 4 || tmonth == 6 || tmonth == 9 || tmonth == 11))) {
            $(".sc-body").removeClass('showmonth5');
            $('.sc-body').addClass('showmonth6');
        } else {
            $('.sc-body').addClass('showmonth5');
            $(".sc-body").removeClass('showmonth6');
        }
        $(".swiper_matter").show();
        $(".botarr").hide();
        $(".annImg").hide();
        $(".calendar_share").hide()
    })


});

//显示选择日期当天的活动
function announceList(v) {
    console.log(v)
    var dayNum = v.find(".day").text();
    var year = $('.sc-select-year').text();
    var monthCH = $('.sc-select-month').text();
    //显示为周几
    var date = year + '/' + monthCH + '/' + dayNum;
    var timeC = Date.parse(date);
    console.log(timeC)
    console.log(date)
    //parse 只能是字符串
    var day = new Date(Date.parse(date));
    var week = today[day.getDay()];
    $(".week_span").text(week);
    // $(".sc-item").data("date",date);
    if (v.children().hasClass('sc-mark-show')) {
        //年
        var year = $('.sc-select-year').text();
        var monthCH = $('.sc-select-month').text();
        //月
        $(".month_span").text(monthCH)
        //日
        var day = v.children()[1].innerText;
        var month = SimpleCalendar.prototype.languageData.months_CH.indexOf(monthCH) + 1;
        month = month + '';
        if (month.length < 2) {
            month = '0' + month;
        }
        day = day + '';
        if (day.length < 2) {
            day = '0' + day;
        }
        //获取标记的日期
        var date = year + '/' + month + '/' + day;
        //获取数组中的值
        var content = mark[date];
        var matterHtml = '';
        //循环标记的内容，添加到页面中
        for (var i = 0; i < content.length; i++) {
            matterHtml +=
                '<div class="swiper-slide slide_s">'
                + '<img src="' + content[i].posterImg + '" alt="宣品缩略图" class="scroll_img">'
                + '</div>';
        }
        $('.mywrap').html(matterHtml);
        $('.clickbig').show();
        //初始化swiper;
        swiper = new Swiper('.swiper-container', {
            slidesPerView: 'auto',
            spaceBetween: 10,
            initialSlide: 0,
            freeMode: true, // slide 是否贴合侧边
            observer: true,//修改swiper自己或子元素时，自动初始化swiper
            observeParents: true//修改swiper的父元素时，自动初始化swiper
        });
        //点击slide,查看大图
        $(".slide_s").click(function (event) {
            $(".botarr").show();
            var hrightitem = $(".sc-item").height();
            // item5Timer = setTimeout(function () {
            //     $(".sc-body").animate({height: hrightitem}, 500);
            // }, 100);
            $(".sc-body").addClass("showmonth7")
            bigImg(this, ".imgbig");
            $(".calendar_share").show()
        })
        $('.matter').html("");
    } else {
        var matterHtmla = ''
        var matterHtmlb = ''
        if (v.hasClass("sc-today")) {
            //今天
            matterHtmla += '<div class="announceItem">' +
                '<div class="clearfix">' +
                '<p class="announceContent fl">今天已有<span>10</span>位老师完成打卡</p>' +
                '<img src="../res/daySignPosters-v2/images/insisticon.png" alt="" class="insisticon fr">' +
                '</div>' +
                '</div>';
            $('.matter').html(matterHtmla);
        } else {
            //不是今天
            matterHtmlb += '<div class="announceItem">' +
                '<div class="clearfix">' +
                '<p class="announceContent fl">当天已有<span>2</span>位老师完成打卡</p>' +
                '<img src="../res/daySignPosters-v2/images/insisticon.png" alt="" class="insisticon fr">' +
                '</div>' +
                '</div>';
            $('.matter').html(matterHtmlb);
        }
        $('.mywrap').html("");
        $('.clickbig').hide();
    }
    //获取日小于10前面补"0"
    if (dayNum < 10) {
        $(".day_span").text("0" + dayNum);
    }
    if (dayNum >= 10) {
        $(".day_span").text(dayNum);
    }
    var seleDay = $(".day_span").text();
    //获取当天的日
    // var curDay = $(".sc-today .day").text();
    var tyear = new Date().getFullYear();
    var tmonth = new Date().getMonth() + 1;
    var tday = new Date().getDate();
    if (tmonth < 10) {
        tmonth = "0" + tmonth;
    }
    if (tmonth >= 10) {
        tmonth = tmonth;
    }
    var tdate = tyear + '/' + tmonth + '/' + tday;
    var ttime = Date.parse(tdate);

    //如果选择到的日期和当天的日前一样，就显示"今",否则不显示
    if (timeC == ttime) {
        $(".today_span").show();
    } else {
        $(".today_span").hide();
    }
}

//查看大图,赋值src
function bigImg(ele, src) {
    var ele = $(ele);
    var imgSrc = ele.find("img").attr("src");
    $(src).attr("src", imgSrc)
    $(src).parent().show();
    ele.parent().parent().parent().hide();
}